package com.qf.dao.impl;

import com.qf.dao.ProductDao;
import com.qf.model.Card;
import com.qf.model.MyProduct;
import com.qf.model.Product;
import com.qf.model.pojo.MyPro;
import com.qf.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ProductDaoImpl extends BaseDao implements ProductDao {
     QueryRunner queryRunner=new QueryRunner(DruidUtil.getDataSource());
    //查询所有理财产品
    @Override
    public List<Product> findAll() {
        String sql="select * from product";
        List<Product> products = queryForList(Product.class, sql);
        return products;
    }

    //根据id查询理财产品信息
    @Override
    public Product findByPid(int pid) {
        String sql="select * from product where p_id=?";
        return queryForOne(Product.class,sql,pid);
    }

    //添加我的产品信息
    @Override
    public int addMyProduct(Connection conn,MyProduct myProduct) {
        try {
            String sql="insert into myproduct(p_id,u_id,share,date,capital,money) values(?,?,?,?,?,?)";
            int num=queryRunner.update(conn,sql,myProduct.getP_id(),myProduct.getU_id(),myProduct.getShare(),myProduct.getDate(),myProduct.getCapital(),myProduct.getMoney());
            return num;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }


    //app扣款  事务
    @Override
    public int lessenUserMoney(Connection conn, int uid, double money) {
        try {
            String sql="update user set u_balance=u_balance-? where u_id=?";
            int update = queryRunner.update(conn, sql, money, uid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    //app收款 事务

    @Override
    public int addUserMoney(Connection conn, int uid, double money) {
        try {
            String sql="update user set u_balance=u_balance+? where u_id=?";
            int update = queryRunner.update(conn, sql, money, uid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    //银行卡扣款 事务
    @Override
    public int lessenCardMoney(Connection conn, String cid, double money) {
        try {
            String sql="update card set c_balance=c_balance-? where c_id=?";
            int update = queryRunner.update(conn, sql, money, cid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    //银行卡加钱 事务

    @Override
    public int addCardMoney(Connection conn, String cid, double money) {
        try {
            String sql="update card set c_balance=c_balance+? where c_id=?";
            int update = queryRunner.update(conn, sql, money, cid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    //根据用户id 查询我的理财信息
    @Override
    public List<MyPro> findMyProByUid(int uid) {
        String sql="select * from product a inner join myproduct b on a.p_id=b.p_id where b.u_id=? and b.money>0";
        return queryForList(MyPro.class,sql,uid);
    }
    //根据用户id 查询已审核银行卡信息

    @Override
    public List<Card> findCardByUid(int uid) {
        String sql="select * from card where c_check=1 and c_type=0 and u_id=?";
        return queryForList(Card.class,sql,uid);
    }
    //根据我的产品id 查询出已经购买的我的产品信息

    @Override
    public MyProduct findMyProById(int id) {
        String sql="select * from myproduct where id=?";
        return queryForOne(MyProduct.class,sql,id);
    }

    //修改我的产品信息  事务

    @Override
    public int updateMyPro(Connection conn, MyProduct myProduct) {
        try {
            String sql="update myproduct set date=?,money=0 where id=?";
            int update = queryRunner.update(conn, sql,myProduct.getDate(),myProduct.getId());
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
    //贷款平台收款 事务
    @Override
    public int addLoansMoney(Connection conn, int lid, double money) {
        try {
            String sql="update loans set money=money+? where l_id=?";
            int update = queryRunner.update(conn, sql, money, lid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    //贷款平台扣款 事务
    @Override
    public int lessLoansMoney(Connection conn, int lid, double money) {
        try {
            String sql="update loans set money=money-? where l_id=?";
            int update = queryRunner.update(conn, sql, money, lid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
